﻿using Dapper;
using Himp.TaskScheduling.Hangfire.Models;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Options;
using System.Data;

namespace Himp.TaskScheduling.Hangfire.Jobs
{
    public class TBPreactionJob
    {
        #region SQL

        public const string GET_BOLTINFOVIEW_SQL = @"
            SELECT *
            FROM [dbo].[BoltInfoView]
            WHERE CONVERT(VARCHAR(10), [date], 120) = CONVERT(VARCHAR(10), GETDATE(), 120)
            ";

        public const string INSERT_SAMPLECABINET_RECORD = @"
            INSERT INTO
            [dbo].[SampleCabinet_ConfigInfo_Record]
            (
            ID,
            SampleCabNum,
            SampleCabCategory,
            SampleTypeDefault,
            SamplePositionNum,
            ValiFlag,
            Samplematrix,
            SampleTypeCurrent,
            XPosition,
            YPosition,
            ZPosition,
            CoalSampleCode,
            Occupancy,
            SampleCustodian,
            SamplingTime,
            SamplingLedgerNum,
            QualityIncoming,
            Granularity,
            Supervisor,
            StartTime,
            EndTime,
            TaskType,
            AutoMode
            )
            VALUES
            (
            @ID,
            @SampleCabNum,
            @SampleCabCategory,
            @SampleTypeDefault,
            @SamplePositionNum,
            @ValiFlag,
            @Samplematrix,
            @SampleTypeCurrent,
            @XPosition,
            @YPosition,
            @ZPosition,
            @CoalSampleCode,
            @Occupancy,
            @SampleCustodian,
            @SamplingTime,
            @SamplingLedgerNum,
            @QualityIncoming,
            @Granularity,
            @Supervisor,
            @StartTime,
            @EndTime,
            @TaskType,
            @AutoMode
            )
            ";

        public const string UPDATE_SAMPLECABINET_RECORD = @"
            UPDATE 
            [dbo].[SampleCabinet_ConfigInfo_Record]
            SET
            SampleCabNum=@SampleCabNum,
            SampleCabCategory=@SampleCabCategory,
            SampleTypeDefault=@SampleTypeDefault,
            SamplePositionNum=@SamplePositionNum,
            ValiFlag=@ValiFlag,
            Samplematrix=@Samplematrix,
            SampleTypeCurrent=@SampleTypeCurrent,
            XPosition=@XPosition,
            YPosition=@YPosition,
            ZPosition=@ZPosition,
            CoalSampleCode=@CoalSampleCode,
            Occupancy=@Occupancy,
            SampleCustodian=@SampleCustodian,
            SamplingTime=@SamplingTime,
            SamplingLedgerNum=@SamplingLedgerNum,
            QualityIncoming=@QualityIncoming,
            Granularity=@Granularity,
            Supervisor=@Supervisor,
            StartTime=@StartTime,
            EndTime=@EndTime,
            TaskType=@TaskType,
            AutoMode=@AutoMode
            WHERE
            ID=@ID
";

        public const string EXIST_FLAG_SQL = @"
            SELECT CASE WHEN 
            EXISTS (SELECT 1 FROM [dbo].[SampleCabinet_ConfigInfo_Record] WHERE ID = @ID) 
            THEN 1 
            ELSE 0 
            END";

        #endregion

        #region 字段

        private readonly ConnectionStringOption _connectionStrings;
        private ILogger<BoltInfo1Job> _logger;

        #endregion

        #region 方法

        public TBPreactionJob(ILogger<BoltInfo1Job> logger
            , IOptions<ConnectionStringOption> connectionStrings)
        {
            _logger = logger;
            _connectionStrings = connectionStrings.Value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("--------启动:自动存样柜信息同步--------");

            try
            {
                using (IDbConnection connection = new SqlConnection(_connectionStrings.SampleCabinet1))
                {
                    connection.Open();
                    // 读取数据
                    var boltInfoList = connection.Query<BoltInfoView>(GET_BOLTINFOVIEW_SQL);

                    foreach (var item in boltInfoList)
                    {
                        bool exists = await connection.ExecuteScalarAsync<bool>(EXIST_FLAG_SQL,
                              new { ID = item.Bolt_ID });

                        //设备编码转换
                        int machineCode = 0;

                        SampleCabinetConfigInfo sampleCabinetConfigInfoRec = new SampleCabinetConfigInfo();
                        //sampleCabinetConfigInfoRec.ID = item.Bolt_ID.ToString();
                        sampleCabinetConfigInfoRec.SampleCabNum = machineCode;
                        sampleCabinetConfigInfoRec.SampleCabCategory = 0;
                        sampleCabinetConfigInfoRec.SampleTypeDefault = 0; //todo


                        if (!exists)
                        {

                        }
                        else
                        {

                        }


                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(exception: e, "异常:自动存样柜信息同步");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task StopAsync(CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }

        #endregion
    }
}
